package com.odm.communitymanage.ui.mine.MyActivity

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.core.bean.MyActivities
import com.odm.communitymanage.core.net.ApiService
import com.odm.communitymanage.ui.activity.ActivitiesAdapter
import com.odm.communitymanage.ui.activity.ActivitiesDiffCallback
import com.odm.communitymanage.ui.activity.ActivityDetailFragment
import com.rxjava.rxlife.lifeOnMain
import com.zhengsr.tablib.view.adapter.TabFlowAdapter
import com.zhengsr.tablib.view.flow.TabFlowLayout
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_latest_activities.*
import kotlinx.android.synthetic.main.fragment_my_activity.*
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 我的活动 列表页面
 * @author: ODM
 * @date: 2020/4/20
 */
class MyActivityFragment : BaseFragment() {

    private var rvAdapter: ActivitiesAdapter?= null


    private var disposable : Disposable?= null


    override fun onDestroyView() {
        super.onDestroyView()
        if(disposable?.isDisposed == false){
            disposable?.dispose()
        }
    }


    override fun initViews() {

        initTabLayout()

        initRecyclerView()

        ivBack_MyActivity.setOnClickListener {
            pop()
        }
    }



    private fun  initRecyclerView(){
        rvMyActivity?.layoutManager = LinearLayoutManager(requireContext())
        rvAdapter = ActivitiesAdapter(mutableListOf())
        rvMyActivity?.adapter  = rvAdapter
//        rvAdapter?.animationEnable = true
//        rvAdapter?.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn)
        //item 的点击事件
        rvAdapter?.setOnItemClickListener{ adapter, view, position ->
            //传递数据和事件--我的活动
            LiveEventBus.get(Constants.EVENT_MY_ACTIVITY_DETAIL)
                .post(rvAdapter?.data?.get(position))

            start(ActivityDetailFragment())
        }
    }




    /**
     * 初始化 TabLayout
     */
    private fun initTabLayout(){
        //TabLayout点击事件
        tabMyActivity?.configTabLayoutConfig {
            //选中项的view的回调
            onSelectViewChange = { fromView, selectViewList, reselect ->
            }
            //选中项index的回调事件--显示List
            onSelectIndexChange = { fromIndex, selectIndexList, reselect ->
                //toIndex 为 选中 Index
                val toIndex = selectIndexList.first()
                //加载数据状态布局
                when(toIndex){
                    0 -> {
                        msv_my_activity.showLoading()
                        getMyPostActivityList()
                    }
                    1 -> {
                        msv_my_activity.showLoading()
                        getMySignUpActivityList()
                    }
                    else -> {}
                }
            }
        }
    }
    


    /**
     *  获取我的发布的 活动列表
     */
    private fun getMyPostActivityList(){
        disposable  = RxHttp.get(ApiService.MY_POST_ACTIVITY)
            .asClass(MyActivities::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(rvAdapter == null){
                    initRecyclerView()
                }
                //返回数据有异常情况
                if(it?.code != 1){
                    ToastUtils.showShort(it.message)
                }
                if(it?.data?.isNotEmpty() == true){
                    msv_my_activity?.showContent()
                    if(rvAdapter?.data?.isEmpty() == true){
                        rvAdapter?.setList(it.data.toMutableList())
                    }else {
                        rvAdapter?.setDiffNewData(DiffUtil.calculateDiff(ActivitiesDiffCallback(rvAdapter?.data?.toList(), it.data)) ,
                            it.data.toMutableList())
                    }
                }else {
                    msv_my_activity?.showEmpty()
                }

            } , {
                //显示错误状态布局
                msv_my_activity.showError()
                ToastUtils.showShort(it?.message)
            })
    }

    /**
     * 获取我的报名的活动的列表
     */
    private fun getMySignUpActivityList(){
        disposable  = RxHttp.get(ApiService.MY_SIGN_UP_ACTIVITY)
            .asClass(MyActivities::class.java)
            .lifeOnMain(this)
            .subscribe({
                if(rvAdapter == null){
                    initRecyclerView()
                }
                //返回数据有异常
                if(it?.code != 1){
                    ToastUtils.showShort(it.message)
                }
                if(it?.data?.isNotEmpty() == true){
                    msv_my_activity?.showContent()
                    if(rvAdapter?.data?.isEmpty() == true){
                        rvAdapter?.setList(it.data.toMutableList())
                    }else {
                        rvAdapter?.setDiffNewData(DiffUtil.calculateDiff(ActivitiesDiffCallback(rvAdapter?.data?.toList(), it.data)) ,
                            it.data.toMutableList())
                    }
                }else {
                    msv_my_activity?.showEmpty()
                }

            } , {
                //显示错误状态布局
                msv_my_activity.showError()
                ToastUtils.showShort(it?.message)
            })
    }



    override fun getLayoutId(): Int {
        return R.layout.fragment_my_activity
    }

}